package com.grad.why.studentsystem.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class addimage {

    public static void main(String[] args) {
        String ftppath="";
        // TODO Auto-generated method stub
        try {
            getFile("D:\\StudentPic");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static String getFile(final String ftppath) throws FileNotFoundException, IOException{
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                WatchKey key;
                try {
                    WatchService watchService = FileSystems.getDefault().newWatchService();
                    Paths.get(ftppath).register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
                    while (true) {
                        File file = new File(ftppath);//ftppath为监听ftp文件夹路径
                        File[] files = file.listFiles();
                        System.out.println("等待图片加载！");
								 /* String fileName = ftppath+"/";//+event.context();
								  System.out.println("增加文件的文件夹路径"+fileName);
								  File file1 = files[files.length-1];//获取最新文件
								  System.out.println(file1.getName());//根据后缀判断*/
                        key = watchService.take();//没有文件增加时，阻塞在这里
                        for (WatchEvent<?> event : key.pollEvents()) {
                            String fileName = ftppath+"/"+event.context();
                            System.out.println("增加文件的文件夹路径"+fileName);
                            File file1 = files[files.length-1];//获取最新文件
                            System.out.println(1+""+file1.getName());//根据后缀判断
                            System.out.println(fileName);
                            getFilesIncludeImages(fileName);
                            //url = uploadFile(file1,uploadAddres);//上传服务器
                            String ftppath1 =fileName;
                            System.out.println("ftppath1:"+ftppath);
                        }if (!key.reset()) {
                            break; //中断循环
                        }

                    }
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, 5000 , 100000000);//第一个数字1000表示，1000ms以后开启定时器,第二个数字3000，表示3000ms后运行一次run

        return ftppath;

    }
    public static ArrayList<String> getFilesIncludeImages(String path) {
        try{
            //此程序是查询每个文件中的jpg图片，如果文件夹下没有图片则resultlist返回空。
            //path = path.replaceAll("\\\\","/");/*用/代替\\*/
            //path = path.replaceAll("\\","/");/*用/代替\*/
            ArrayList<String> resultList = new ArrayList<String>();
            File file = new File(path);
            if(file.isDirectory()){
                File[] files = file.listFiles();
                for(File f : files){
                    System.out.println(" "+f.getAbsolutePath());
                    String regex = ".+\\.[j][p][g]";//正则表达式 用来判读是否是图片格式的文件
                    //System.out.println("f.getPath:"+f.getPath()+"\n");
                    if(f.isDirectory() ) {
                        getFilesIncludeImages(f.getAbsolutePath());
                        resultList = new ArrayList<String>();
                    }else if (f.isFile() &&f.getName().matches(regex)) {
                        resultList.add(f.getAbsolutePath());
                        System.out.println("f.getAbsolutePath:"+f.getAbsolutePath()+"\n");
                        String imagename=f.getName();
                        System.out.println("imagename:"+imagename+"\n");
                    }

                }
                if(resultList.isEmpty()==false){  //	排除resultList是空集的情况
                    System.out.println("resultList:"+resultList+"\n");
                    return resultList;
                }
            }else{

            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
